/*
 * 监控区主页的主区域, 默认, 工程播放列表页
 * 命名空间 "project-main"
 * 
 * @author Zdream
 * @since 0.2.0
 */

monitor.ui.body.addContent({
	name : "project-main",
	
	init : function (argv) {
		var sid = argv.sid;
		var data = monitor.store.screens[sid];
		this.state = {
			sid : sid,
			data : {}
		};
		this.resetStateData();
		
		// monitor.store
		// vm create
		this.vm = new Vue({
			el: "#mnt_body",
			data: this.state
		});

		// 消息监听
		var thiz = this;
		monitor.msgCenter.subscribe(
				["aside-remove-button", "aside-refresh-button", "status-change", "query-data-project"],
		function(event, argv) {
			switch (event) {
			case "aside-remove-button":
			case "aside-refresh-button":
			case "status-change":
			{
				thiz.onRefreshButton(argv);
			} break;
			case "query-data-project":
			{
				thiz.handleQueryProject(argv);
			} break;
			}
		}, this.name);
	},
	
	onRefreshButton : function (data) {
		if (data.sid != this.state.sid) {
			return;
		}
		this.resetStateData();
		this.vm.$forceUpdate();
	},
	
	resetStateData : function () {
		var dest = this.state.data;
		
		// 清空 dest
		for (var key in dest) {
			delete dest[key];
		}
		
		// 源数据 (不允许变动)
		var source = monitor.store.screens[this.state.sid];
		if (source != null) {
			// 重置
			for (var key in source) {
				dest[key] = source[key];
			}
			
			// pname 单独设置
			if (dest.pid != null) {
				dest.pname = dest.pid;
				if (dest.pid != null) {
					var project = monitor.store.queryProject(dest.pid);
					if (project != null) {
						dest.pname = project.name;
					}
				}
			}
		}
	},
	
	handleQueryProject : function (data) {
		var pid = data.pid;
		
		if (pid != this.state.data.pid) {
			return;
		}
		
		this.state.data.pname = data.name;
		this.vm.$forceUpdate();
	},
	
	destroy : function () {
		this.state = null;
		
		// 删除消息监听
		monitor.msgCenter.remove(this.name);
		
		// vm destroy
		if (this.vm != null)
			this.vm.$destroy();
		this.vm = null;
	}
	
});
